package com.cyy.cn.service;


import com.cyy.cn.dao.SpitDao;
import com.cyy.cn.pojo.Spit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import util.IdWorker;


import java.util.Date;
import java.util.List;

/**
 * spit服务层
 * 
 * @author Administrator
 *
 */
@Service
public class SpitService {

	@Autowired
	private SpitDao spitDao;
	
	@Autowired
	private IdWorker idWorker;

	@Autowired
	private MongoTemplate mongoTemplate;

	/**
	 * 查询全部记录
	 */
	public List<Spit> findAll(){
		return spitDao.findAll();
	}

	/**
	 * 根据主键查询实体
	 */
	public Spit findById(String id){
		Spit spit = spitDao.findById(id).get();
		return spit;
	}

	/**
	 * 增加
	 *//*
	public void add(Spit spit){
		spit.set_id(idWorker.nextId() + "");//主键值
		spitDao.save(spit);
	}*/

	/**
	 * 修改
	 */
	public void  update(Spit spit){
		spitDao.save(spit);
	}

	/**
	 * 删除
	 */
	public void deleteById(String id){
		spitDao.deleteById(id);
	}

	public Page<Spit> findByParentId(String parentId,int page,int size){
		PageRequest request = PageRequest.of(page-1,size);
		return  spitDao.findByParentid(parentId,request);
	}

	//吐槽点赞
	/*public void updateThumbup(String id){
		Spit spit = spitDao.findById(id).get();
		spit.setThumbup(spit.getThumbup() == null ? 0 : spit.getThumbup() + 1);
		spitDao.save(spit);
	}*/

	public void updateThumbup(String id){
		Query query = new Query();
		query.addCriteria(Criteria.where("_id").is(id));
		Update update = new Update();
		update.inc("thumbup",1);
		mongoTemplate.updateFirst(query,update,"spit");
	}

	/**
	 * 发布吐槽
	 * @param spit
	 */
	public void add(Spit spit){
		spit.set_id( idWorker.nextId()+"" );
		spit.setPublishtime(new Date());//发布日期
		spit.setOrderNo(0);//浏览量
		spit.setShare(0);//分享数
		spit.setThumbup(0);//点赞数
		spit.setComment(0);//回复数
		// spit.setState("1");//状态
		if(spit.getParentid()!=null && !"".equals(spit.getParentid())){// 如果存在上级ID,评论
			Query query=new Query();
			query.addCriteria(Criteria.where("_id").is(spit.getParentid()));
			Update update=new Update(); update.inc("comment",1);
			mongoTemplate.updateFirst(query,update,"spit");
		}
		spitDao.save(spit);
	}

}
